package com.ruoyi.jl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.jl.domain.GiveGold;
import com.ruoyi.jl.domain.Player;
import com.ruoyi.jl.domain.custome.CustomPlayer;
import com.ruoyi.jl.mapper.GameConfigMapper;
import com.ruoyi.jl.mapper.GiveGoldMapper;
import com.ruoyi.jl.mapper.PlayerMapper;
import com.ruoyi.jl.service.IPlayerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 玩家 服务层实现
 *
 * @author wh
 * @date 2019-04-29
 */
@Slf4j
@Service
public class PlayerServiceImpl extends ServiceImpl<PlayerMapper, Player> implements IPlayerService {
    @Autowired
    PlayerMapper playerMapper;

    @Autowired
    GameConfigMapper gameConfigMapper;

    @Autowired
    GiveGoldMapper giveGoldMapper;

    @Override
    public List<CustomPlayer> list(Player player) {
        List<CustomPlayer> list = playerMapper.list(player);
        return list;
    }

    /**
     * 改变玩家的状态
     *
     * @param player
     * @return
     */
    @Override
    public int changeStatus(Player player) {
        return playerMapper.updateById(player);
    }

    /**
     * 玩家赠送金币
     *
     * @param giveId
     * @param receiveId
     * @param goldNum
     * @param ipAddr
     * @param safePwd
     */
    @Transactional
    @Override
    public Integer PlayGiveGold(Integer giveId, Integer receiveId, Integer goldNum,
                                String ipAddr, String safePwd) throws CustomException {
        // 判断密码是否正确
        Player player = playerMapper.selectById(giveId);
        Integer safeGold = player.getSafeGold();// 银行金币数量
        String storeSafePwd = player.getSafePwd();// 银行密码
        Player receivePlayer = playerMapper.selectById(receiveId);// 接收玩家
        if (!storeSafePwd.equals(safePwd)) {// 密码错误
            throw CustomException.instance(1, "密码错误!");
        }
        if (safeGold < goldNum) {// 判断银行金币是否充足
            throw CustomException.instance(2, "银行金币不足!");
        }
        if (receivePlayer == null) {// 接收玩家不存在
            throw CustomException.instance(3, "接收玩家不存在!");
        }
        giveGold(giveId, receiveId, goldNum, ipAddr, 1);
        return 0;
    }

    /**
     * 赠送金币
     *
     * @param giveId
     * @param receiveId
     * @param goldNum
     * @param ipAddr
     * @param type      0系统赠送,1玩家赠送
     */
    @Transactional
    @Override
    public void giveGold(Integer giveId, Integer receiveId, Integer goldNum, String ipAddr, Integer type) {
        Map<String, Integer> param = new HashMap<>();
        param.put("goldNum", goldNum);
        if (type == 1) {
            // 扣除库存
            param.put("playerId", giveId);
            playerMapper.reduceGold(param);
        }
        // 添加用户账户金币
        param.put("playerId", receiveId);
        playerMapper.addGold(param);
        // 添加赠送金币记录
        GiveGold giveGold = new GiveGold();
        giveGold.setGiveId(giveId);
        giveGold.setReceiveId(receiveId);
        giveGold.setGoldNum(goldNum);
        giveGold.setType(type);
        giveGold.setIpAddress(ipAddr);
        giveGold.setGiveTime(new Date());
        giveGoldMapper.insert(giveGold);
    }

    @Override
    public CustomPlayer queryEditInfo(Integer playerId) {
        CustomPlayer player = playerMapper.queryEditInfo(playerId);
        return player;
    }

    @Override
    @Transactional
    public void updatePlayer(CustomPlayer player) {
        Integer slotType = player.getSlotType();
        Integer playerId = player.getPlayerId();
        playerMapper.updateById(player);
        gameConfigMapper.updateSlotType(slotType, playerId);
    }

    @Override
    public Player queryPlayer(String playerId) {
        Map<String, Object> map = new HashMap<>();
        map.put("playerId", playerId);
        Player storePlayer = playerMapper.queryPlayerInfo(map);
        return storePlayer;
    }

    @Override
    public Player queryPlayerInfo(Player player) {
        String wxOpenId = player.getWxOpenid();
        // 判断该用户是否存在
        Map<String, Object> map = new HashMap<>();
        map.put("wxOpenId", wxOpenId);
        Player storePlayer = playerMapper.queryPlayerInfo(map);
        if (storePlayer == null) {// 不存在,添加记录
            player.setCarryGold(0);
            player.setSafeGold(0);
            player.setSafePwd("123456");
            player.setUseupGold(0);
            player.setDifficultLevel(3);
            player.setIsOnline(0);
            player.setVipLevel(0);
            player.setCreateTime(new Date());
            player.setLoginTime(new Date());
            player.setStatus(0);
            playerMapper.insert(player);
            map.put("playerId", player.getPlayerId());
            storePlayer = playerMapper.queryPlayerInfo(map);
        } else {
            Player p = new Player();
            p.setPlayerId(storePlayer.getPlayerId());
            // 已注册用户更新字段
            p.setIsOnline(0);// 在线
            p.setToken(player.getToken());// token
            playerMapper.updateById(p);
            storePlayer.setToken(player.getToken());
        }
        return storePlayer;
    }

    @Override
    public void updatePlayerInfo(Player player, String newToken) {
        boolean flag = false;
        UpdateWrapper wrapper = new UpdateWrapper();
        if (StringUtils.isNotEmpty(newToken)) {
            wrapper.set("token", newToken);
            flag = true;
        }
        if (StringUtils.isNotEmpty(player.getCarryGold())) {
            wrapper.set("carry_gold", player.getCarryGold());
            flag = true;
        }
        if (StringUtils.isNotEmpty(player.getIsOnline())) {
            wrapper.set("is_online", player.getIsOnline());
            flag = true;
        }
        if (flag) {
            wrapper.eq("player_id", player.getPlayerId());
            this.update(wrapper);
        }

    }

    @Override
    public void updateSafeGold(Integer playerId, String oldSafePwd, String newSafePwd) throws CustomException {
        Player player = playerMapper.selectById(playerId);
        String safePwd = player.getSafePwd();
        if (!safePwd.equals(oldSafePwd)) {
            throw CustomException.instance(500, "密码不匹配");
        }
        player = new Player();
        player.setPlayerId(playerId);
        player.setSafePwd(newSafePwd);
        playerMapper.updateById(player);
    }

    @Transactional(rollbackFor = CustomException.class)
    @Override
    public int customQueryWrapper(Player player) throws CustomException {
        QueryWrapper<Player> wrapper = new QueryWrapper<>();
        // mysqlMapper.getAll(Wrappers.<MysqlData>lambdaQuery().eq(MysqlData::getGroup, 1));
        wrapper.eq("avatar", "aaa");
        wrapper.eq("user_name", "bbb");
        List<Player> all = playerMapper.getAll(wrapper);
        System.out.println(all.toString());
        playerMapper.updateById(player);
        System.out.println(1/0);
        throw new CustomException().instance(2, "事务测试!");
    }

}
